# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = 'restructuredtext en'
__all__ = []

from ... import formlib as layout
from ... import hint
from ... import pilelib as stack
from ...forms import ganjifa as glayout
from ...gamelib import info as gi, game
from ...packlib import info as cs
from ...utillib.misc import kwdefault
from .. import wavemotion as wstack

GT, SL = gi.GT, gi.SL
Wrap = stack.StackWrapper


class MughalCircles(cs.Mughal_Deck, game.Game):
    """
    Finish 1 deck on _foundation_.

    Layout
      - row [24 wheel-shape N,  offset]
      - talon [1 W W foundation, no offset]
      - foundation [4 vert E, 4 vert W, no offset]
      - reserve [1 vert E, 1 vert W S foundation, no offset]

    :CVariables:
      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

      _foundation_ : pysollib.stack.Stack
        .. pysollib.stack.SS_Foundation (max_move=0)

        Build up in rank by same suit. Play none. Base ace.

      _row_ : pysollib.stack.Stack
        .. pysollib.stack.SS_RowStack (max_move=1, max_accept=1)

        Init 1 card. Build down in rank by same suit. Play top.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=14401, name='Mughal Circles',
                    category=cs.TYPE_MUGHAL_GANJIFA, game_type=GT.ONE_DECK,
                    decks=1, skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _talon_ = stack.InitialDealTalon
    _foundation_ = Wrap(stack.SS_Foundation, max_move=0)
    _row_ = Wrap(stack.SS_RowStack, max_accept=1, max_move=1)
    _reserve_ = stack.ReserveStack
    _hint_ = hint.CautiousDefaultHint

    def _create_game(self, **kw):
        l, s = layout.Layout(self), self.s
        self.table_size = (l.XM + l.XS * 9, l.YM + l.YS * 7,)
        ddx, ddy = self.width / 2 - l.HXS, self.hieght / 2 - l.HYS
        for dx, dy in ((-10, 0,), (-8, -8,), (0, -10,), (8, -8,), (10, 0,),
                       (8, 8,), (0, 10,), (-8, 8,), (-20, 0,), (-19, -8,),
                       (-15, -15,), (-8, -19,), (0, -20,), (8, -19,),
                       (15, -15,), (19, -8,), (20, 0,), (19, 8,), (15, 15,),
                       (8, 19,), (0, 20,), (-8, 19,), (-15, 15,), (-19, 8,),):
            x, y = l.delta_coords(x=ddx, y=ddy, dx=dx, dy=dy, fx=10, fy=10)
            s['rows'].append(self._row_(x, y, game=self, offset=(0, 3,)))
        self._set_region(s['rows'], l._region(x=l.XM + l.XS,
                                           w=self.width - l.XS - l.XM))
        half = l.NSUIT / 2
        suit = l.SUIT[:half]
        for x in (l.XM, self.width - l.XS):
            for y in zip(l.col_iter(l.YM, npiles=half), suit):
                s['foundations'].append(self._foundation_(x, y, game=self,
                                                       suit=suit))
            s['reserves'].append(self._reserve_(x, self.height - l.YS,
                                             game=self))
            suit = l.SUIT[half:]
        s['talon'] = self._talon_(l.XM + l.XS, l.YM, game=self)
        l.defaultStackGroups()
        return l

    def _start(self, rows=4, **kw):
        """
        Deal initial cards for new game.
          - s[rows]: 4 ea.

        .. \_start([rows=4]) -> None
        """
        for i in range(rows - 1):
            self.s['talon'].dealRow(frames=0)
        self.s['talon'].dealRow()


class EightLegions(cs.Mughal_Deck, game.Game):
    """
    Arrange 1 deck on row.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.ganjifa.CornerReserveLayout
        - row [8 horz N, vert offset]
        - talon [1 SW, no offset]
        - reserve [5 vert, 5 horz SE, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.DealRowTalon

        Deal 1 card per row. No redeals.

      _row_ : pysollib.stack.Stack
        .. pysollib.games.wavemotion.RK_RowStack (base_rank=cs.RAJA,
          max_move=12, max_cards=99)
        Init 5 card. Build down in rank by same suit. Play seq with max 12
        card. Base raja.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=14404, name='Eight Legions',
                    category=cs.TYPE_MUGHAL_GANJIFA, game_type=GT.SPIDER,
                    decks=1, skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _layout_ = glayout.CornerReserveLayout
    _talon_ = stack.DealRowTalon
    _row_ = Wrap(wstack.RK_RowStack, base_rank=cs.RAJA, max_move=12,
                 max_cards=99)
    _reserve_ = stack.ReserveStack

    def _create_game(self, **kw):
        kwdefault(kw, rows=10, reserves=10)
        return super(EightLegions, self)._create_game(**kw)

    def _start(self, rows=5, **kw):
        """
        Deal initial cards for new game.
          - s[rows]: 5 ea up.

        .. \_start([rows=5]) -> None
        """
        for i in range(rows - 1):
            self.s['talon'].dealRow(frames=0)
        self.s['talon'].dealRow()

    @property
    def is_complete(self):
        return all(s.is_filled for s in self.s['rows'] if s.cards)


PROVIDES = [MughalCircles, EightLegions]